/*

    tools23d - tools for converting images to 3D formats.
    Copyright (C) 2014  Dave Gorst

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sysexits.h>
#include <time.h>
#include "tools23d.h"
#include "system.h"

/**
Check if terminal is interactive or if we are running from a script (POSIX version).

@return ::TRUE if terminal is interactive.
*/

BOOL T3D_IsInteractive (void)
{
	return (isatty (fileno (stdin)));
}

/**
Return the number of usable cores (Linux version).

@return	Cores currently available.
*/

unsigned int T3D_GetNumberOfCPUs (void)
{
	#ifdef __linux__

        	/* Linux / MacOSX 10.5+ (glibc) */

       		 return sysconf(_SC_NPROCESSORS_ONLN);
	#else

		return 1;

	#endif
}

/**
Convert internal ::t3d_return code to system-specific version (POSIX)

@param	my_code	Input ::t3d_return code
@return POSIX specific return code.
*/

int T3D_GetSystemReturnCode (t3d_return my_code)
{
	switch (my_code)
	{
	case t3d_success : return EXIT_SUCCESS; break;
	case t3d_bad_parameter : return EX_USAGE; break;
	case t3d_no_input : return EX_NOINPUT; break;
	case t3d_operation_failed : return EX_SOFTWARE; break;
	case t3d_displayhelp : return EXIT_SUCCESS; break;
	default : return EXIT_FAILURE; break;
	}
}

/**
Threadsafe version of localtime (POSIX).

@param  time_in_secs	Input value giving time in seconds since Jan 1 1970.
@param  write_time	Pointer to previously allocated tm struct for output.
@return T3D return code.
*/

t3d_return T3D_GetLocaltime (time_t time_in_secs, struct tm * write_time)
{
	return (localtime_r (&time_in_secs, write_time) != NULL) ? t3d_success : t3d_operation_failed;
}
